<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-button type="info" @click="jumpToSave">新增</el-button>
      </el-form-item>

      <el-form-item label="大分类">
        <el-select placeholder="请选择大分类" v-model="dictionaryQuery.largeClassification" @change="getClassificationList()">
          <el-option v-for="(option,index) in largeList"
                     :value="option"
                     :key="index"
          >{{option}}
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="中分类">
        <el-select placeholder="请选择中分类" v-model="dictionaryQuery.middleClassification">
          <el-option v-for="(middleOption,middleIndex) in middleList"
                     v-if="middleOption.lev == 2"
                     :value="middleOption.text"
                     :key="middleIndex"
          >{{middleOption.text}}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="小分类">
        <el-select placeholder="请选择小分类" v-model="dictionaryQuery.miniClassification">
          <el-option v-for="(miniOption,miniIndex) in miniList"
                     v-if="miniOption.lev == 3"
                     :value="miniOption.text"
                     :key="miniIndex"
          >{{miniOption.text}}
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          @click="getDictionaryList()"
        >查询
        </el-button
        >
        <el-button type="success" @click="reflush()"
        >重置
        </el-button
        >
        <el-button type="danger" @click="removeAll()" :disabled="this.multipleSelection.length==0"
        >删除
        </el-button
        >

      </el-form-item>
    </el-form>

    <el-table
      :data="dictionaryList"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column prop="id" label="序号"  align="center" width="70">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        type="selection" align="center">
      </el-table-column>

      <el-table-column width="300" label="上级名称" align="center">
        <template slot-scope="scope" >
          {{scope.row.ptext===null?'无':scope.row.ptext}}
        </template>
      </el-table-column>

      <el-table-column prop="text" label="文本" align="center" >
      </el-table-column>

      <el-table-column label="是否启用" align="center">
        <template slot-scope="scope">
          {{scope.row.isdisable == 0 ?'未启用':'启用'}}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <router-link :to="'/basic_setting/edit_dictionary/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
            >修改
            </el-button>
          </router-link>
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="remove(scope.row.id)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="total, prev, pager, next, jumper"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      align="center"
      style="padding: 30px 0 ; text-align: center"
      @current-change="getDictionaryList"
    >
    </el-pagination>

  </div>
</template>

<script>
  import api_basic_setting from "@/api/dictionary";

  export default {
    name: "system_dictionary",
    data() {
      return {
        currentPage: 1,
        pageSize: 5,
        total: 0,
        dictionaryQuery: {
          largeClassification: "",
          middleClassification: "",
          miniClassification: ""
        },

        totalList: [],
        largeList: [],
        middleList: [],
        miniList: [],
        dictionaryList: [],
        multipleSelection: []
      }
    },
    created() {
      this.getLargeList()
    },
    methods: {
      removeAll(){
        console.log(this.multipleSelection)
        this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          this.multipleSelection.forEach((item,i,multipleSelection) =>{
            api_basic_setting.remove(item.id).then((Response) => {
              this.getDictionaryList();
            });
            this.$message({
              type: "success",
              message: "删除成功!",
            });
        })
      })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      getDictionaryList(currentPage = 1) {
        this.currentPage = currentPage;
        api_basic_setting.getDictionaryList(this.currentPage, this.pageSize, this.dictionaryQuery)
          .then(Response => {
            this.dictionaryList = Response.data.list;
            this.total = Response.data.total;
          })
      },
      getLargeList() {
        api_basic_setting.getLarge()
          .then(Response => {
            this.largeList = Response.data.largeList;
            this.totalList = Response.data.list;
            this.getDictionaryList();

          })
      },
      getClassificationList() {
        this.dictionaryQuery.middleClassification = "";
        this.dictionaryQuery.miniClassification = "";
        api_basic_setting.getClassificationList(this.dictionaryQuery.largeClassification)
          .then(Response => {
            this.middleList = Response.data.middleList;
            this.miniList = Response.data.miniList;
          })
      },
      reflush() {
        this.dictionaryQuery.miniClassification = "";
        this.dictionaryQuery.middleClassification = "";
        this.dictionaryQuery.largeClassification = "";
        this.dictionaryList = [];
        this.getLargeList();
      },
      jumpToSave() {
        this.$router.push({path: "/basic_setting/save_dictionary"});
      },
      remove(id) {
        this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          api_basic_setting.remove(id).then((Response) => {
            this.getDictionaryList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
      },
    }

  }
</script>

<style scoped>

</style>
